<?php
class Con_phone extends Acan{
	public $defaultAct='index';
	function __construct($ainfo=null){
		parent::__construct($ainfo=null);
	}
	function Act_test(){
		$model = Afun_db()->model('nice9s.acan_data_phone');
		$rs = $model->ofind('');
		print_r($rs);
	}
	function Act_index(){
		$this->atitle = '手机号码归属地查询 - 手机定位 - 美妙人生';
		$this->keywords = '手机,手机号码,手机定位,手机跟踪';
		$this->description = '手机号码归属地查询官方版,手机在线拥有最全的手机号码数据库,手机定位查归属地就上http://aban.nice9s.com/tool-phone';
		include $this->atpl();
	}
	function Act_api(){
		$this->atitle = '手机号码归属地API接口 - 号码归属地数据库 - 手机号码归属地数据库';
		$this->keywords = '手机,手机号码,手机号码归属地查询';
		$this->description = '美妙人生提供的手机号码归属地查询系统免费API接口程序,号码归属地数据库,最新手机号码归属地数据库';
		include $this->atpl();
	}
	function Act_get($m=false,$type='jsonp'){
		if(in_array($_GET['t'],array('json','jsonp'))) $type = $_GET['t'];
		if($m){
			$mobile = $m;
		}else{
			header('Access-Control-Allow-Origin: *');
			$mobile = $_GET['m'];
		}
		$num = strlen($mobile);
		if($num!=7 && $num!=11){
			$data->msg = '号码错误';
			$data->status = 0;
			return $this->rstype($data,$type);
		}
		$pcode = substr($mobile,0,7);
		$data = Afun_cache_get('Atool:pcode:'.$pcode);
		if(empty($data)){
			$data = Afun_db()->model(Acan_table_data_phone)->ofind('pid='.$pcode);
			if(!$data){
				$data = $this->guishu($pcode);
			}else{
				unset($data->id,$data->area);
				$data->status = 1;
			}
			unset($data->postcode,$data->areacode);
			Afun_cache_set('Atool:pcode:'.$pcode,$data,86400*7);
		}
		$data->mobile = $mobile;
		return $this->rstype($data,$type);
	}
	function Act_gets(){
		$type = 'jsonp';
		if(in_array($_GET['t'],array('json','jsonp'))) $type = $_GET['t'];
		header('Access-Control-Allow-Origin: *');
		$mobiles = $_GET['m'];
		$mrr = explode('_',$mobiles);
		foreach($mrr as $k=>$v){
			$data[] = $this->Act_get($v,'obj');
		}
		$this->rstype($data,$type);
	}
	function rstype($obj,$type='obj'){
		if($type=='obj'){
			return $obj;
		}elseif($type=='jsonp'){
			Afun_jsonp($obj,true);
		}elseif($type=='json'){
			Afun_json($obj,true);
		}
	}
	function guishu($pcode){
		$url = "http://api.showji.com/Locating/www.showji.com.aspx?m=$pcode&output=json";
		$curl = Afun_curl('get',$url);
		$obj = json_decode($curl['data']);
		if($obj->QueryResult!='False'){
			$data->pid			=$pcode;
			$data->type		=$obj->Corp;
			$data->city			=$obj->City;
			$data->province	=$obj->Province;
			$data->areacode	=$obj->AreaCode;
			$data->postcode	=$obj->PostCode;
			Afun_db()->model(Acan_table_data_phone)->save($data);
			$data->status = 1;
		}else{
			$data->msg = '号码错误';
			$data->status = 0;
		}
		return $data;
	}
	function Act_zheng(){
		return;
		$model = Afun_db()->model(Acan_table_data_phone);
		$con->where = 'city is null';
		$con->group = 'area';
		$con->limit = '0,100';
		$data = $model->ofindAll($con);
		echo '<table>';
		if($data)
		foreach($data as $k=>$v){
			$arr = explode('省',$v->area);
			$sdata = array();
			if(count($arr)==1){
				$len = strlen($arr[0]);
				if(in_array(substr($arr[0],0,9),array('内蒙古','重庆市'))){
					$sdata['province'] = substr($arr[0],0,9);
					if(substr($arr[0],0,9)=='重庆市'){
						$sdata['province'] = '重庆';
					}
					if(substr($arr[0],-3)=='市'){
						$sdata['city'] = substr($arr[0],9,-3);
					}else{
						$sdata['city'] = substr($arr[0],9);
					}
				}elseif(in_array(substr($arr[0],0,6),array('青海','西藏','新疆','宁夏'))){
					$sdata['province'] = substr($arr[0],0,6);
					if(substr($arr[0],-3)=='市'){
						$sdata['city'] = substr($arr[0],6,-3);
					}else{
						$sdata['city'] = substr($arr[0],6);
					}
				}elseif(substr($arr[0],-3)=='市'){
					$sdata['province'] = $sdata['city'] = substr($arr[0],0,-3);
				}
				$num = $model->updateAll($sdata,"area='".$v->area."' and city is null");
				echo '<tr><td>'.$v->area.'</td><td>变省:'.$sdata['province'].'</td><td>市:'.$sdata['city'].'</td><td>更新'.$num.'条</td></tr>';
			}elseif(count($arr)==2){
				$sdata['province'] = $arr[0];
				if(substr($arr[1],-3)=='市'){
					$sdata['city'] = substr($arr[1],0,-3);
				}else{
					$sdata['city'] = $arr[1];
				}
				$num = $model->updateAll($sdata,"area='".$v->area."' and city is null");
				echo '<tr><td>'.$v->area.'</td><td>变省:'.$sdata['province'].'</td><td>市:'.$sdata['city'].'</td><td>更新'.$num.'条</td></tr>';
			}
		}
		echo '</table>';
	}
}
?>